<template>
<div>
  <el-row>
    <bread-crumb first="员工管理"></bread-crumb>
  </el-row>
  <el-row>
    <el-form class="demo-ruleForm" label-width="80px">
      <el-col :span="5">
        <el-form-item label="姓名">
          <el-input v-model="query.name"/>
        </el-form-item>
      </el-col>
      <el-col :span="4">
        <el-form-item label="部门">
          <el-select v-model="query.deptType">
            <el-option v-for="(deptType,index) in query.typeList"
                       :label="deptType.type"
                       :value="deptType.id"
                       :key="index"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="1">
        <el-form-item>
          <el-button type="success" @click="queryList({
          name:query.name,
          deptType:query.deptType})">
            <i class="fa fa-search" aria-hidden="true">搜索</i>
          </el-button>
        </el-form-item>
      </el-col>
    </el-form>
  </el-row>
  <el-row>
    <el-button-group style="float: left">
      <el-button @click="dialog.dialogFormVisible=true" type="success">
        <i class="fa fa-plus" aria-hidden="true"> 添加</i></el-button>
      <el-button type="warning"><i class="fa fa-pencil" aria-hidden="true"> 修改</i></el-button>
      <el-button type="danger"><i class="fa fa-trash-o" aria-hidden="true"> 删除</i></el-button>
    </el-button-group>
    <el-table :data="list.slice((currpage - 1) * pagesize, currpage * pagesize)" border style="width: 100%">
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="sex" label="性别"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="deptType.type" label="部门"></el-table-column>
      <el-table-column prop="position" label="职务"></el-table-column>
      <el-table-column prop="usable" label="是否启用">
        <template slot-scope="scope">
          <i class="fa" :class="scope.row.usable|changeBoolToTag"></i>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template>
          <el-button-group>
            <el-button plain type="warning" size="small">修改</el-button>
            <el-button plain type="danger" size="small">删除</el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
  </el-row>
  <el-row>
    <el-pagination
      background
      :current-page="currpage"
      layout="prev, pager, next, sizes, total, jumper"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pagesize"
      :total="list.length"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    >
    </el-pagination>
  </el-row>

  <el-dialog :visible.sync="dialog.dialogFormVisible" title="添加用户">
    <el-form label-width="80px">
      <el-form-item label="用户名">
        <el-input v-model="dialog.user.name"></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-input v-model="dialog.user.sex"></el-input>
      </el-form-item>
      <el-form-item label="年龄">
        <el-input v-model="dialog.user.age"></el-input>
      </el-form-item>
      <el-form-item label="部门">
        <el-select v-model="dialog.user.deptType" style="width:100%">
           <el-option :key="index"
                      :label="deptType.type"
                      :value="deptType.id" v-for="(deptType,index) in query.typeList"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="职位">
        <el-input v-model="dialog.user.position"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="insertHandle">保存</el-button>
      </el-form-item>

    </el-form>
  </el-dialog>
</div>
</template>
<script>
  import BreadCrumb from '../../components/ Breadcrumb/indexBreadcrumd'
    export default {
        name: "employee",
        data:function () {
            return{
              pagesize:5,
              currpage: 1,
              list:[],
              query:{
                deptType:'',
                name:'',
                typeList:[]
              },dialog:{
                dialogFormVisible:false,
                user:{
                  name:'',
                  sex:'',
                  age:'',
                  deptType:'',
                  position:''
                }
              }

            }
        },
      components: {
          BreadCrumb
      }, mounted() {

          this.queryList();

          this.initDeptType();
      },methods:{
          queryList:function(user){
            let _this=this;
            this.axios({
              url:'/mock/employee/list',
              method:'get'
            }).then(function (res) {
              _this.list=res.data.list;
              /*alert(name);*/            //****

              /*alert(user.name);
              alert(user.deptType);*/
              if(user){
                _this.list=_this.list.filter((item)=>{
                  /*
                  1.实现模糊查询
                  2.实现多条件查询
                   */
                  return item.name.includes(user.name)&&item.deptType.id==user.deptType;
               /*return item.name==name;*/
                })
              }

            });
          },
        queryForSave:function(user){ //***
          let _this=this;
          this.axios({
            url:'/mock/employee/list',
            method:'get'
          }).then(function (res) {
            _this.list=res.data.list;
            _this.list.unshift(user);  //***
            _this.dialog.dialogFormVisible=false;
          })

        },
          initDeptType:function () {
            let _this=this;
             this.axios({
               url:'/mock/employee/deptTypes',
               method: 'get'
             }).then((res)=>{
               _this.query.typeList=res.data;
             })
          },insertHandle:function () {

          const deptTypes=[
            {
              id:1,
              type:'人事部'
            },{
              id:2,
              type:'财务部'
            },{
              id:3,
              type:'技术部'
            }
          ];

          this.dialog.user.deptType=deptTypes.filter((item)=>{
              return item.id==this.dialog.user.deptType;
            })[0];

            this.queryForSave(this.dialog.user);

        },handleCurrentChange(cpage) {
        this.currpage = cpage;
      },
      handleSizeChange(psize) {
        this.pagesize = psize;
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;

      },
      }
    }
</script>

<style scoped>
  .el-row{
    margin-bottom: 20px;
  }
  .background{
    color: lightgreen;
  }

</style>
